<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= puAliasIns();
	$process_unit = sxREQUEST("process_unit");
	$alias = sxREQUEST("alias");
	$entity = sxREQUEST("entity");
	$description = sxREQUEST("description");
	$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
	$note = sxREQUEST("note");
}else{
	$process_unit = "";
	$alias = "";
	$entity = "";
	$description = "";
	$read_only = "";
	$note = "";
}
//carico tutte le entity
$sSQL = "SELECT * FROM entity ORDER BY entity";
if(!$objSDAO->query($sSQL, "ent")){
	$errMsg .= _t("Impossibile caricare l'elenco delle entity<br>");
}
if(isset($_REQUEST["pu"])){
	$process_unit = $_REQUEST["pu"];
}elseif($process_unit == ""){
	$errMsg .= _t("Il processo non &egrave; definito<br>");
}
$sLettAlias = "ABCDEFGHIJKLMNOPQ";
$sSQL = "SELECT a.alias FROM pu_alias a WHERE a.process_unit = '".$process_unit."'";
$objSDAO->query($sSQL,'alidis');
while($rowAD=$objSDAO->fetch('alidis')) {
	$sLettAlias = str_replace($rowAD['alias'],'',$sLettAlias);
}

//output HTML
$aOpz = array("tit2"=>_t("Crereqtype nuovo alias"), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			var objAlias =  document.getElementById("alias");
			var objMessArea =  document.getElementById("messArea");
			var errMsg = "";
			if(objAlias.value == ""){
				errMsg += "<?php echo _t("L'alias &egrave; obbligatorio.")?><br>";
				objAlias.focus();
			}
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save new alias");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"puAliasIns.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"process_unit\" id=\"process_unit\" value=\"$process_unit\">");
//output comandi
$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati del nuovo alias"),
										"text"=>_t("salva")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0)
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Alias")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("processo")." (*)</td>" .
			"<td class=\"text01\">$process_unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("alias")." (*)</td>" .
			"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"alias\" id=\"alias\">");
for($i=0; $i<strlen($sLettAlias); $i++){
	$slc = (sxREQUEST('alias') == $sLettAlias[$i]) ? ' SELECTED' : '' ;
	echo("<option value=\"".$sLettAlias[$i]."\"$slc>".$sLettAlias[$i]."</option>");
}
echo("</select>");	
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("entity")." (*)</td>" .
			"<td class=\"text01\">");
echo("<select name=\"entity\" id=\"entity\">");
while($row = $objSDAO->fetch("ent")){
	$sel = "";
	if($entity == $row["entity"])
		$sel = "SELECTED";
	echo("<option value=\"".$row["entity"]."\" $sel>".$row["entity"]."</option>");
}
echo("</select>");
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
$chk = "";
if($read_only == "Y")
	$chk = "CHECKED";
echo("<tr>" .
			"<td class=\"labl01\">"._t("sola lettura")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"checkbox\" name=\"read_only\" id=\"read_only\" $chk></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puAliasIns($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	if(sxREQUEST("entity") == "" and sxREQUEST("alias") < "W")
		$errMsg .= _t("L'entity &egrave; obbligatoria").".<br>";
	if(sxREQUEST("entity") != "" and sxREQUEST("alias") >= "W")
		$errMsg .= _t("L'entity non deve essere definita").".<br>";
	
	
	//controllo che l'alias non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_alias WHERE process_unit='".$_REQUEST["process_unit"]."' AND alias='".$_REQUEST["alias"]."'";
	$num = 0;
	if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("test")){
			$num = $row["num"]; 
		}
	}else{
		$errMsg .= $objSDAO->error;
	}
	if($num != 0)
		$errMsg .= _t("L'alias")." '".$_REQUEST["alias"]."' "._t("esiste gi&agrave;.")."<br>";
	if($errMsg == ""){		
		$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
		$sSQL = "INSERT INTO " .
				"pu_alias " .
				"(process_unit, " .
				"alias, " .
				"entity, " .
				"description, " .
				"read_only, " .
				"note)" .
				"VALUES('".sxRAS("process_unit")."', " .
				"'".strtoupper(addslashes(sxGPC(trim($_REQUEST["alias"]))))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["entity"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"'$read_only', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$process_unit = sxREQUEST("process_unit");
			$_SESSION[USEROBJ]->reLoad();
			logRecord("300", _t("Inserito nuovo alias nel processo")." '".$process_unit."'");
			//serverRedir("puAliasList.php?pu=$process_unit&xsn=".XSN);
			if($redirOnEnd){
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);
			}			
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}

